package info.nene_loveplus.jsp;

import info.nene_loveplus.util.WikiUtil;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import com.google.appengine.api.datastore.Text;

public class WikiTag extends TagSupport {
	private static final long serialVersionUID = 1L;

	private Object wiki;

	public void setWiki(Object wiki) {
		this.wiki = wiki;
	}

	@Override
	public int doStartTag() throws JspException {
		if (wiki == null) {
			return SKIP_BODY;
		}
		Text wikiText;
		if (wiki instanceof String) {
			wikiText = new Text((String) wiki);
		} else if (wiki instanceof Text) {
			wikiText = (Text) wiki;
		} else {
			return SKIP_BODY;
		}

		try {
			pageContext.getOut().write(WikiUtil.toHtml(wikiText.getValue()));
			return SKIP_BODY;
		} catch (IOException e) {
			throw new JspException(e);
		}
	}

}
